//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package gitlab_triger

import (
	"encoding/json"
	"fmt"

	"atomgit.com/openlinksaas-org/api-server/triger_impl/core"
)

type IssueObjectAttrInfo struct {
	CreatedAt string `json:"created_at"`
	UpdatedAt string `json:"updated_at"`
	Title     string `json:"title"`
	Action    string `json:"action"`
	State     string `json:"state"`
	Url       string `json:"url"`
}

type IssueHookInfo struct {
	User       UserInfo            `json:"user"`
	Project    ProjectInfo         `json:"project"`
	ObjectAttr IssueObjectAttrInfo `json:"object_attributes"`
}

func ExtractIssueHookAttr(data []byte) ([]string, error) {
	obj := &IssueHookInfo{}

	err := json.Unmarshal(data, obj)
	if err != nil {
		return nil, err
	}

	retList := []string{
		fmt.Sprintf("%s=%s", core.GIT_HOOK_TYPE, "issue"),
		fmt.Sprintf("%s=%s", core.GIT_NAME, obj.Project.Name),
		fmt.Sprintf("%s=%s", core.GIT_WEB_URL, obj.Project.WebUrl),
		fmt.Sprintf("%s=%s", core.GIT_HTTP_URL, obj.Project.GitHttpUrl),
		fmt.Sprintf("%s=%s", core.GIT_SSH_URL, obj.Project.GitSshUrl),
		fmt.Sprintf("%s=%s", core.GIT_USERNAME, obj.User.Username),

		fmt.Sprintf("%s=%s", core.GIT_ISSUE_CREATE_AT, obj.ObjectAttr.CreatedAt),
		fmt.Sprintf("%s=%s", core.GIT_ISSUE_UPDATE_AT, obj.ObjectAttr.UpdatedAt),
		fmt.Sprintf("%s=%s", core.GIT_ISSUE_TITLE, obj.ObjectAttr.Title),
		fmt.Sprintf("%s=%s", core.GIT_ISSUE_ACTION, obj.ObjectAttr.Action),
		fmt.Sprintf("%s=%s", core.GIT_ISSUE_STATE, obj.ObjectAttr.State),
		fmt.Sprintf("%s=%s", core.GIT_ISSUE_URL, obj.ObjectAttr.Url),
	}
	return retList, nil
}
